package luongvd.weather.common;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

@SuppressLint("SimpleDateFormat") 
public class Utils {

	public static String createFormattedAddressFromAddress(final Address myAddress,StringBuilder mSb) {
		if (myAddress==null) {
			return "@";
		}else {
			mSb.setLength(0);
			String getAddressLine = "";
			if(myAddress.getAddressLine(0) != null)
			{
				getAddressLine = myAddress.getAddressLine(0)+" ";
			}
			
			String getAdminArea = "";
			if(myAddress.getAdminArea() != null)
			{
				getAdminArea = myAddress.getAdminArea()+" ";
			}
			String getSubAdminArea = "";
			if(myAddress.getSubAdminArea() != null)
			{
				getSubAdminArea = myAddress.getSubAdminArea()+" ";
			}
			String getCountryName = "";
			if(myAddress.getCountryName() != null)
			{
				getCountryName = myAddress.getCountryName();
			}
			
			mSb.append(getAddressLine).append(getSubAdminArea).append(getAdminArea).append(getCountryName);
//			final int addressLineSize = address.getMaxAddressLineIndex();
//			for (int i = 0; i < addressLineSize; i++) {
//				mSb.append(address.getAddressLine(i));
//				if (i != addressLineSize - 1) {
//					mSb.append(", ");
//				}
//			}
			return mSb.toString();
		}

	}
	
	//method get address from location
	public static String getAddressFromLocation(Location location, Context context)
	{
		String address_str = null;
		if (location != null) {
			Geocoder geoCoder = new Geocoder(context);
			try {
				List<Address> addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
				Log.d("Luong", "=============================== so item address: "+addresses.size());
				if (addresses.size() > 0) {
					Address myAddress = addresses.get(0);
					StringBuilder strmyLocation = new StringBuilder();
					String getAddressLine = "";
					if(myAddress.getAddressLine(0) != null)
					{
						getAddressLine = myAddress.getAddressLine(0)+" ";
					}
					
					String getAdminArea = "";
					if(myAddress.getAdminArea() != null)
					{
						getAdminArea = myAddress.getAdminArea()+" ";
					}
					String getSubAdminArea = "";
					if(myAddress.getSubAdminArea() != null)
					{
						getSubAdminArea = myAddress.getSubAdminArea()+" ";
					}
					String getCountryName = "";
					if(myAddress.getCountryName() != null)
					{
						getCountryName = myAddress.getCountryName();
					}
					
					strmyLocation.append(getAddressLine).append(getSubAdminArea).append(getAdminArea).append(getCountryName);
					address_str = strmyLocation.toString();
				} 
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return address_str;
	}
	
	//method get my location
	public static Location getMyLocation(Context context)
	{
		LocationManager mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
	    Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

	    long GPSLocationTime = 0;
	    if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); }

	    long NetLocationTime = 0;

	    if (null != locationNet) {
	        NetLocationTime = locationNet.getTime();
	    }

	    if ( 0 < GPSLocationTime - NetLocationTime ) {
	        return locationGPS;
	    }
	    else{
	        return locationNet;
	    }
	}
	
	//method convert int unixtime to time
	public static String convertUnixtimeToTime(long unixSeconds)
	{
		Date date = new Date(unixSeconds*1000); // *1000 is to convert seconds to milliseconds
		SimpleDateFormat sdf = new SimpleDateFormat("EE, dd/MM/yyyy"); // the format of your date
		String formattedDate = sdf.format(date);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+7"));
		return formattedDate;
	}
	//method convert int unixtime to time
	public static String convertUnixtimeToDayOfMonth(long unixSeconds)
	{
		Date date = new Date(unixSeconds*1000); // *1000 is to convert seconds to milliseconds
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM"); // the format of your date
		String formattedDate = sdf.format(date);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+7"));
		return formattedDate;
	}
	//method convert int unixtime to time
	public static String convertUnixtimeToDayOfWeek(long unixSeconds)
	{
		Date date = new Date(unixSeconds*1000); // *1000 is to convert seconds to milliseconds
		SimpleDateFormat sdf = new SimpleDateFormat("EE"); // the format of your date
		String formattedDate = sdf.format(date);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+7"));
		return formattedDate;
	}
	
	//method get bitmap for imageview
	public static Bitmap getBitmap(Context context, String name_icon)
	{
		InputStream  bitmap = null;
		if(Utils.checkContainsCharacter(name_icon,"d"))
		{
			try {
				bitmap = context.getAssets().open("icon/day/"+name_icon);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else
		{
			try {
				bitmap = context.getAssets().open("icon/night/"+name_icon);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Bitmap bit=BitmapFactory.decodeStream(bitmap);
		return bit;
	}
	
	//method check String contains character
	public static Boolean checkContainsCharacter(String s, String check_character)
	{
		for(int i=0; i< s.length(); i++)
		{
			String c = s.substring(i,i+1);
			if(c.matches(check_character))
			{
				return true;
			}
		}
		return false;
	}
}
